package com.situ.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.situ.entity.Role;
import com.situ.service.IRoleService;
import com.situ.service.impl.RoleServiceImpl;

@WebServlet("/role")
public class RoleServlet extends HttpServlet {

	private static final long serialVersionUID = 7352040655238757034L;

	IRoleService roleService = new RoleServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		switch (action) {
		case "listAll":
			listAll(request, response);
			break;
		}
	}

	private void listAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		List<Role> list = roleService.selectAllRoles();
		String listStr = JSON.toJSONString(list);
		response.getWriter().write(listStr);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
